package com.hospicpc.demos.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.hospicpc.demos.entity.Messages;
import com.hospicpc.demos.mapper.MessagesMapper;
import com.hospicpc.demos.mapper.UserInfoMapper;
import com.hospicpc.demos.service.MessagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lse
 * @since 2024-03-18
 */
@Service
public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, Messages> implements MessagesService {
    @Autowired
    private MessagesMapper messagesMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void insertMessage(Integer userId, String messageId, String messageData, String createTime, String nickName) {
        Messages messages = new Messages();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        messages.setSendtime(LocalDateTime.parse(createTime, formatter));
        messages.setSenderid(Long.valueOf(userId));
        messages.setReceiverid(Long.valueOf(messageId));
        messages.setMessagecontent(messageData);
        messages.setMessageid(IdUtil.simpleUUID());
        messages.setAvatarUrl(userInfoMapper.getUserAvatar(messageId));
        messagesMapper.insertMessages(messages);
    }

    @Override
    public List<Messages> getMessagesLists(String userId) {
        return messagesMapper.getMessagesLists(userInfoMapper.selectUserIdById(userId));
    }

    @Override
    public List<Messages> getMessages(String sendId, String userId) {
        return messagesMapper.getMessages(userInfoMapper.selectUserIdById(sendId),userId);
    }

    @Override
    public void isReadMessages(String sendId, String userId) {
        messagesMapper.isReadMessages(userInfoMapper.selectUserIdById(sendId),userId);
    }
}
